package ui;

import java.awt.Color;
import java.util.Scanner;

import net.jcip.annotations.Immutable;
import shape.Shape;
import shape.ShapeColourDecorator;
import shape.ShapeTextDecorator;
import storage.CareTaker;
import surface.Canvas;
import brush.BrushFactory;
import brush.BrushStateContext;

import com.google.inject.Inject;

@Immutable
public class Cli {

	private final CareTaker careTaker;
	private final Canvas canvas;
	private final View view;
	private final Scanner input = new Scanner(System.in);

	@Inject
	public Cli(final CareTaker careTaker, final Canvas canvas) {
		this.careTaker = careTaker;
		this.canvas = canvas;
		this.view = View.newInstance(canvas);
		canvas.attach(view);
	}

	
	private String readInput() {
		return input.nextLine();
	}

	
	public void mainMenu() {
		String readIn = "";

		do {
			System.out.println("\nSelect operation: " 
					+ "\n (d)raw shape"
					+ "\n (q)uit" 
					+ "\n (v)iew canvas" 
					+ "\n (u)ndo"
					+ "\n (c)olour shape"
					+ "\n (w)rite on shape");

			readIn = readInput();
			switch (readIn) {
			case "d":
				drawShape();
				break;
			case "v":
				canvas.displayShapes();
				break;
			case "u":
				canvas.restoreMemento(careTaker.remove());
				break;
			case "c":
				colourShape();
				break;
			case "w":
				writeOnShape();
				break;
			}
		} while (!readIn.equalsIgnoreCase("q"));
	}

	
	private void drawShape() {
		BrushStateContext context = null;

		System.out.println("Type of shape: " 
						+ "\n (s)quare" 
						+ "\n (c)ircle"
						+ "\n (m)ain menu");

		String readIn = readInput();

		switch (readIn) {
		case "s":
			System.out.println("Enter size: ");
			int size = Integer.parseInt(readInput());
			context = BrushFactory.squareBrush(canvas, size, size, size);
			context.draw();
			break;
		case "c":
			System.out.println("Enter radius: ");
			size = Integer.parseInt(readInput());
			context = BrushFactory.circleBrush(canvas, size, size, size);
			context.draw();
			break;
		case "m":
			mainMenu();
		}
	}

	
	private void colourShape() {
		try {
			System.out.println("select number of shape to colour: ");

			canvas.displayShapes();

			String readIn = readInput();
			int index = Integer.parseInt(readIn) - 1;
			Shape shape = canvas.getShape(index);

			System.out.println("enter colour, red, green, blue : ");
			readIn = readInput();
			switch (readIn) {
			case "red":
				shape = new ShapeColourDecorator(shape, Color.red);
				break;
			case "green":
				shape = new ShapeColourDecorator(shape, Color.green);
				break;
			case "blue":
				shape = new ShapeColourDecorator(shape, Color.blue);
				break;
			}
			
			canvas.updateShape(index, shape);

		} catch (NumberFormatException e) {
			System.out.println("Invalid number");
			colourShape();
		}
	}

	
	private void writeOnShape() {
		try {
			System.out.println("select number of shape to write on: ");

			canvas.displayShapes();

			String readIn = readInput();
			int index = Integer.parseInt(readIn) - 1;
			Shape shape = canvas.getShape(index);

			System.out.println("enter text: ");
			readIn = readInput();
			shape = new ShapeTextDecorator(shape, readIn);
			canvas.updateShape(index, shape);
			
		} catch (NumberFormatException e) {
			System.out.println("Invalid number");
			writeOnShape();
		}
	}
}
